package com.uton.rfidcontroller.customize;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uton.rfidcontroller.R;
import com.uton.rfidcontroller.view.adapter.BankingContentAdapter;
import com.uton.rfidcontroller.view.base.BaseFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 王家宝 on 2017/9/4.
 * Email:639586819@qq.com
 * Tel:18241150668
 */

public class Tablayout extends RelativeLayout {

    @BindView(R.id.svp_up)
    StaticViewPager svpUp;
    @BindView(R.id.svp_down)
    StaticViewPager svpDown;

    //注册RadioGroup
    @BindView(R.id.rbt_left)
    RadioButton leftRbtn;
    @BindView(R.id.rbt_right)
    RadioButton rightRbtn;

    //指示器光标
    @BindView(R.id.tv_indicator_left)
    TextView mIndiLeft;
    @BindView(R.id.tv_indicator_right)
    TextView mIndiRight;

    private SearchView mSearchView;

    private BaseFragment mFragmentLeft;

    private BaseFragment mFragmentRight;

    private int pageNum = 1;

    private ArrayList<Fragment> mFragments;

    private boolean isReverse;

    public Tablayout(Context context) {
        super(context);
    }

    public Tablayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.customize_tablayout, this);
        ButterKnife.bind(this);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Tablayout);
        isReverse = ta.getBoolean(R.styleable.Tablayout_isReverse, false);
        if (isReverse) {
            svpUp.setVisibility(VISIBLE);
            svpDown.setVisibility(GONE);
        } else {
            svpUp.setVisibility(GONE);
            svpDown.setVisibility(VISIBLE);
        }
        ta.recycle();

        initRadioGroup();
    }

    public Tablayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public void setupWithSearchView(SearchView searchView) {
        mSearchView = searchView;
    }


    public StaticViewPager getCurrentViewPager() {
        if (isReverse) {
            return svpUp;
        } else {
            return svpDown;
        }
    }


    public void initViewPager(final BankingContentAdapter pagerAdapter, BaseFragment fragmentLeft, BaseFragment fragmentRight) {

        getCurrentViewPager().addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == 0) {
                    switch (pagerAdapter.getCurrentFragmentId()) {
                        case 0:
                            if (mFragmentLeft != null) {
                                mFragmentLeft.refreshFirst();
                            }
                            break;
                        case 1:
                            if (mFragmentRight != null) {
                                mFragmentRight.refreshFirst();
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        });
        getCurrentViewPager().setOffscreenPageLimit(3);
        getCurrentViewPager().setAdapter(pagerAdapter);
        getCurrentViewPager().setScrollble(false);
        getCurrentViewPager().setCurrentItem(0);
        mFragments = new ArrayList<>();
        mFragmentLeft = fragmentLeft;
        mFragmentRight = fragmentRight;
        mFragments.add(fragmentLeft);
        mFragments.add(fragmentRight);
        pagerAdapter.setData(mFragments);
    }

    /**
     * 初始化RadioGroup
     */
    private void initRadioGroup() {
        getCurrentViewPager().setCurrentItem(0, true);
        mIndiLeft.setVisibility(View.VISIBLE);
        mIndiRight.setVisibility(View.INVISIBLE);

        leftRbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getCurrentViewPager().setCurrentItem(0, true);
                mIndiLeft.setVisibility(View.VISIBLE);
                mIndiRight.setVisibility(View.INVISIBLE);
                if (mSearchView != null) {
                    mSearchView.closeSearch();
                }
                pageNum = 1;
            }
        });

        rightRbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getCurrentViewPager().setCurrentItem(1, true);
                mIndiLeft.setVisibility(View.INVISIBLE);
                mIndiRight.setVisibility(View.VISIBLE);
                if (mSearchView != null) {
                    mSearchView.closeSearch();
                }
                pageNum = 2;
            }
        });
        leftRbtn.setChecked(true);
    }

    /**
     * 设置左侧Tab标题
     *
     * @param StringId
     */
    public void setLeftTitle(int StringId) {
        leftRbtn.setText(StringId);
    }

    /**
     * 设置右侧Tab标题
     *
     * @param StringId
     */
    public void setRightTitle(int StringId) {
        rightRbtn.setText(StringId);
    }


    /**
     * 设置Tab拓展模式
     */
    public void setTabExtraMode() {
        Drawable selectorScan = getResources().getDrawable(R.drawable.selector_tablayout_scan);
        selectorScan.setBounds(0, 0, selectorScan.getMinimumWidth(), selectorScan.getMinimumHeight());
        leftRbtn.setCompoundDrawables(null, selectorScan, null, null);

        Drawable selectorInput = getResources().getDrawable(R.drawable.selector_tablayout_input);
        selectorInput.setBounds(0, 0, selectorInput.getMinimumWidth(), selectorInput.getMinimumHeight());
        rightRbtn.setCompoundDrawables(null, selectorInput, null, null);
    }


}
